/*
	se todo.txt
*/

/* Starting gun: makarov */
secondary_weapons = [
	["makarov", 50],
	["REVOLVER_EP1", 60],
	["ACE_L9A1", 75],
	["ACE_P226", 90],
	["glock17_EP1", 90],
	["Colt1911", 100],
	["ACE_TT", 120],
	["ACE_USP", 150],
	["ACE_Glock18", 150],
	["UZI_EP1", 180]
];

primary_weapons = [
	["LeeEnfield", 300],
	["AKS_74_U", 500],
	["ACE_UMP45", 550],
	["ACE_MP5A4", 650],
	["ACE_MP7", 800],
	["M1014", 1050],
	["Saiga12K", 1100],
	["ACE_SPAS12", 800],
	["M16A2", 1500],
	["ACE_SKS", 1850],
	["Sa58V_EP1", 2200],
	["ACE_oc14", 2800],
	["ACE_M3A1", 1200],
	["ACE_KAC_PDW", 1800],
	["ACE_G3A3", 3600],
	["ACE_AK74M_GL_Kobra", 4200],
	["ACE_HK416_D10", 4600],
	["ACE_AKMS_SD", 6500],
	["ACE_HK417_micro", 7500]
];

prefMags = [
	["Saiga12K", "8Rnd_B_Saiga12_Pellets"],
	["M1014", "ACE_8Rnd_12Ga_Buck00"],
	["ACE_SPAS12", "ACE_8Rnd_12Ga_Buck00"]
];


_jip = false;
if (isNull player) then {
	_jip = true;
};

/* Compile functions */
call compile preprocessFileLineNumbers "functions.sqf";


/* Load the positions array for all clients */
Null = [] execVM "positions.sqf";

if (isNil "initComplete") then {
	initComplete = false;
};

if (isNil "matchInProgress") then {
	matchInProgress = false;
};

if (isNil "ROUND_WIN") then {
	ROUND_WIN = 0;
};

if (isNil "bombLocked") then {
	bombLocked = false;
};

if (isServer) then {
	/* Server initialization */

	// Select the first area to play at
	[] call fnc_setup_match;

	waitUntil { !(isNil "currentPosition") };

	initComplete = true;
	publicVariable "initComplete";
};

waitUntil { initComplete };
waitUntil { time > 0 };

/* Run for all players */
eh_killed = [];

"eh_killed" addPublicVariableEventHandler { Null = (_this select 1) spawn fnc_player_killed; };
"ROUND_WIN" addPublicVariableEventHandler { [] call fnc_eh_win };

if !(isDedicated) then {
	/* Set up event handlers */
	player addEventHandler ["respawn", { Null = [] spawn fnc_eh_respawn; }];
	player addEventHandler ["killed", { Null = (_this) spawn fnc_eh_killed; }];
	"matchInProgress" addPublicVariableEventHandler { Null = [] spawn fnc_begin_match };
	"notify_timelimit" addPublicVariableEventHandler { Null = (_this select 1) spawn fnc_notify_time };	


	/* Give player some starter money */
	player setVariable ["money", 150];
	//player setVariable ["money", 16000];

	/* Clear player */
	removeAllWeapons player;
	removeBackpack player;

	/* Give a standard makarov with 3 magazines */
	for "_x" from 1 to 3 do { player addMagazine "8Rnd_9x18_Makarov"; };
	player addWeapon "Makarov";

	/* Set player game status (0 = dead, 1 = alive) */
	player setVariable ["status", 0, true];
};

/* Init game */
if ((_jip) and !(isDedicated)) then {
	[] spawn {
		waitUntil {!isNull player};
		if (!isServer && (player != player)) then
		{
  			waitUntil {player == player};
		};

		player setPos ([] call fnc_get_spawnPos);
	};
	[] call fnc_eh_respawn;
} else {
	[] call fnc_begin_noMatch;
};